<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Pattern matching</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Pattern_matching"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Pattern_matching rootpage-Pattern_matching skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Pattern matching</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<style data-mw-deduplicate="TemplateStyles:r1236090951">
/* start https://en.wikipedia.org/ */
.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}
/* end https://en.wikipedia.org/ */
</style><div role="note" class="hatnote navigation-not-searchable">This article is about pattern matching in <a href="Functional_programming" title="Functional programming">functional programming</a>. For other uses, see <a href="String_matching" class="mw-redirect" title="String matching">string matching</a> and <a href="Pattern_recognition" title="Pattern recognition">pattern recognition</a>.</div>
<div role="note" class="hatnote navigation-not-searchable">For the use of variable matching criteria in defining abstract patterns to match, see <a href="Regular_expression" title="Regular expression">regular expression</a>.</div>
<style data-mw-deduplicate="TemplateStyles:r1305433154">
/* start https://en.wikipedia.org/ */
.mw-parser-output .ambox{border:1px solid #a2a9b1;border-left:10px solid #36c;background-color:#fbfbfb;box-sizing:border-box}.mw-parser-output .ambox+link+.ambox,.mw-parser-output .ambox+link+style+.ambox,.mw-parser-output .ambox+link+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+style+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+link+.ambox{margin-top:-1px}html body.mediawiki .mw-parser-output .ambox.mbox-small-left{margin:4px 1em 4px 0;overflow:hidden;width:238px;border-collapse:collapse;font-size:88%;line-height:1.25em}.mw-parser-output .ambox-speedy{border-left:10px solid #b32424;background-color:#fee7e6}.mw-parser-output .ambox-delete{border-left:10px solid #b32424}.mw-parser-output .ambox-content{border-left:10px solid #f28500}.mw-parser-output .ambox-style{border-left:10px solid #fc3}.mw-parser-output .ambox-move{border-left:10px solid #9932cc}.mw-parser-output .ambox-protection{border-left:10px solid #a2a9b1}.mw-parser-output .ambox .mbox-text{border:none;padding:0.25em 0.5em;width:100%}.mw-parser-output .ambox .mbox-image{border:none;padding:2px 0 2px 0.5em;text-align:center}.mw-parser-output .ambox .mbox-imageright{border:none;padding:2px 0.5em 2px 0;text-align:center}.mw-parser-output .ambox .mbox-empty-cell{border:none;padding:0;width:1px}.mw-parser-output .ambox .mbox-image-div{width:52px}@media(min-width:720px){.mw-parser-output .ambox{margin:0 10%}}@media print{body.ns-0 .mw-parser-output .ambox{display:none!important}}
/* end https://en.wikipedia.org/ */
</style>
<p>In <a href="Computer_science" title="Computer science">computer science</a>, <b>pattern matching</b> is the act of checking a given sequence of <a href="Lexical_analysis#Token" title="Lexical analysis">tokens</a> for the presence of the constituents of some <a href="Pattern" title="Pattern">pattern</a>. In contrast to <a href="Pattern_recognition" title="Pattern recognition">pattern recognition</a>, the match usually must be exact: "either it will or will not be a match." The patterns generally have the form of either <a href="String_(computer_science)" title="String (computer science)">sequences</a> or <a href="Tree_structure" title="Tree structure">tree structures</a>. Uses of pattern matching include outputting the locations (if any) of a pattern within a token sequence, to output some component of the matched pattern, and to substitute the matching pattern with some other token sequence (i.e., <a href="Regular_expression" title="Regular expression">search and replace</a>).
</p><p>Sequence patterns (e.g., a text string) are often described using <a href="Regular_expression" title="Regular expression">regular expressions</a> and matched using techniques such as <a href="Backtracking" title="Backtracking">backtracking</a>.
</p><p>Tree patterns are used in some <a href="Programming_language" title="Programming language">programming languages</a> as a general tool to process data based on its structure, e.g. <a href="C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a>,<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> <a href="F_Sharp_(programming_language)" title="F Sharp (programming language)">F#</a>,<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> <a href="Haskell" title="Haskell">Haskell</a>,<sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup> <a href="Java_(programming_language)" title="Java (programming language)">Java</a>,<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup> <a href="ML_(programming_language)" title="ML (programming language)">ML</a>, <a href="Python_(programming_language)" title="Python (programming language)">Python</a>,<sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup> <a href="Racket_(programming_language)" title="Racket (programming language)">Racket</a>,<sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> <a href="Ruby_(programming_language)" title="Ruby (programming language)">Ruby</a>,<sup id="cite_ref-7" class="reference"><a href="#cite_note-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup> <a href="Rust_(programming_language)" title="Rust (programming language)">Rust</a>,<sup id="cite_ref-8" class="reference"><a href="#cite_note-8"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup> <a href="Scala_(programming_language)" title="Scala (programming language)">Scala</a>,<sup id="cite_ref-9" class="reference"><a href="#cite_note-9"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup> <a href="Swift_(programming_language)" title="Swift (programming language)">Swift</a><sup id="cite_ref-10" class="reference"><a href="#cite_note-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup> and the symbolic mathematics language <a href="Wolfram_Mathematica" class="mw-redirect" title="Wolfram Mathematica">Mathematica</a> have special <a href="Syntax_(programming_languages)" title="Syntax (programming languages)">syntax</a> for expressing tree patterns and a <a href="Language_construct" title="Language construct">language construct</a> for <a href="Conditional_(computer_programming)" title="Conditional (computer programming)">conditional execution</a> and value retrieval based on it.
</p><p>Often it is possible to give alternative patterns that are tried one by one, which yields a powerful conditional programming construct. Pattern matching sometimes includes support for <a href="Guard_(computer_science)" title="Guard (computer science)">guards</a>.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="History">History</h2></div>
<div role="note" class="hatnote navigation-not-searchable">See also: <a href="Regular_expression#History" title="Regular expression">Regular expression § History</a></div>
<p>Early programming languages with pattern matching constructs include <a href="COMIT" title="COMIT">COMIT</a> (1957), <a href="SNOBOL" title="SNOBOL">SNOBOL</a> (1962), <a href="Refal" title="Refal">Refal</a> (1968) with tree-based pattern matching, <a href="Prolog" title="Prolog">Prolog</a> (1972), St Andrews Static Language (<a href="SASL_(programming_language)" title="SASL (programming language)">SASL</a>) (1976), <a href="NPL_(programming_language)" title="NPL (programming language)">NPL</a> (1977), and <a href="Kent_Recursive_Calculator" title="Kent Recursive Calculator">Kent Recursive Calculator</a> (KRC) (1981).
</p><p>The pattern matching feature of function arguments in the language <a href="ML_(programming_language)" title="ML (programming language)">ML</a> (1973) and its dialect <a href="Standard_ML" title="Standard ML">Standard ML</a> (1983) has been carried over to some other <a href="Functional_programming" title="Functional programming">functional programming</a> languages that were influenced by them, such as <a href="Haskell" title="Haskell">Haskell</a> (1990), <a href="Scala_(programming_language)" title="Scala (programming language)">Scala</a> (2004), and <a href="F_Sharp_(programming_language)" title="F Sharp (programming language)">F#</a> (2005). The pattern matching construct with the <code>match</code> keyword that was introduced in the <a href="ML_(programming_language)" title="ML (programming language)">ML</a> dialect <a href="Caml" title="Caml">Caml</a> (1985) was followed by languages such as <a href="OCaml" title="OCaml">OCaml</a> (1996), <a href="F_Sharp_(programming_language)" title="F Sharp (programming language)">F#</a> (2005), <a href="F*_(programming_language)" title="F* (programming language)">F*</a> (2011), and <a href="Rust_(programming_language)" title="Rust (programming language)">Rust</a> (2015).
</p><p>Many <a href="Text_editor" title="Text editor">text editors</a> support pattern matching of various kinds: the <a href="QED_(text_editor)" title="QED (text editor)">QED editor</a> supports <a href="Regular_expression" title="Regular expression">regular expression</a> search, and some versions of <a href="TECO_(text_editor)" title="TECO (text editor)">TECO</a> support the OR operator in searches.
</p><p><a href="Computer_algebra_system" title="Computer algebra system">Computer algebra systems</a> generally support pattern matching on algebraic expressions.<sup id="cite_ref-11" class="reference"><a href="#cite_note-11"><span class="cite-bracket">[</span>11<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="Terminology">Terminology</h2></div>
<p>Pattern matching involves specialized terminology.
</p>
<dl><dt>Matching</dt>
<dd>The act of comparing a <i>discriminant</i> to a <i>pattern</i> (or collection of patterns), possibly selecting a <i>continuation</i>, extracting <i>bindings</i>, performing a <i>substitution</i>, or any combination of these. Also known as <b>destructuring</b>.</dd></dl>
<dl><dt>Pattern</dt>
<dd>Syntax describing expected structure in the <i>discriminant</i>, plus specification of portions of the discriminant to extract (<i>bindings</i>) or ignore (<i>wildcards</i>). Pattern languages can be rich; see below for terminology denoting specific kinds of pattern.</dd></dl>
<dl><dt>Discriminant</dt>
<dd>The value to be examined and matched against a pattern. In most cases, this will be a data structure of some kind, with type <a href="Duality_(mathematics)" title="Duality (mathematics)">dual to</a> the pattern being applied. Also known as the <b>subject value</b> or <b>scrutinee</b>.</dd></dl>
<dl><dt>Continuation</dt>
<dd>In some languages, when multiple alternative patterns are applied to a discriminant, when one alternative matches, an associated code fragment is executed in an environment extended with the matching pattern's <i>bindings</i>. This code fragment is the <i>continuation</i> associated with the pattern.</dd></dl>
<dl><dt>Substitution</dt>
<dd>Replacement of a portion of a discriminant data structure with some computed value. The computation may depend on the replaced portion of the discriminant as well as on other bindings extracted from the discriminant.</dd></dl>
<div class="mw-heading mw-heading3"><h3 id="Terminology_of_patterns">Terminology of patterns</h3></div>
<p>While some concepts are relatively common to many pattern languages, other pattern languages include unique or unusual extensions.
</p>
<dl><dt><a href="Name_binding" title="Name binding">Binding</a></dt>
<dd>A way of associating a <i>name</i> with a portion of the discriminant, so that the name is <a href="Name_binding" title="Name binding">bound to</a> that portion when the continuation executes. For example, in Rust, <code class="mw-highlight mw-highlight-lang-rust mw-content-ltr" style="" dir="ltr"><span class="k">match</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="o">=></span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span></code> expects <code>v</code> to be a pair, and <code>a</code> and <code>b</code> are bindings bringing variables of the same name into scope in the continuation ("<code>...</code>").</dd></dl>
<dl><dt>Wildcard</dt>
<dd>Often written as a single underscore, <code>_</code>, the wildcard pattern accepts all values without examining them further, ignoring their structure. Also known as <b>discard</b>, the <b>wild pattern</b>, the <b>catch-all pattern</b>, or as a <b>hole</b>.</dd></dl>
<dl><dt><a href="Guard_(computer_science)" title="Guard (computer science)">Guard</a></dt>
<dd>A <i>guard</i> is an expression that must succeed (or yield boolean true) as a final step before considering a pattern to have successfully matched. In some languages (e.g. <a href="Erlang_(programming_language)" title="Erlang (programming language)">Erlang</a>), guards are written using a restricted subset of the full language; in others (e.g. <a href="Haskell" title="Haskell">Haskell</a>), guards may use the full language.</dd></dl>
<dl><dt>Predicate</dt>
<dd>Some pattern languages allow user-defined <i>predicate</i> functions to be embedded in a pattern. The predicate is applied to the portion of the discriminant corresponding to the position of the predicate in the pattern; if the predicate responds with boolean false, the pattern is considered to have failed. For example, in Racket, the pattern <code class="mw-highlight mw-highlight-lang-rkt mw-content-ltr" style="" dir="ltr"><span class="p">(</span><span class="nb">list</span><span class="w"> </span><span class="p">(</span><span class="n">?</span><span class="w"> </span><span class="nb">even?</span><span class="p">)</span><span class="w"> </span><span class="k">...</span><span class="p">)</span></code> first expects a list, and then applies the predicate <code>even?</code> to each element; the overall pattern thus succeeds only when the discriminant is a list of even numbers.</dd></dl>
<dl><dt>View pattern</dt>
<dd>Languages like Haskell<sup id="cite_ref-12" class="reference"><a href="#cite_note-12"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup> and Racket<sup id="cite_ref-13" class="reference"><a href="#cite_note-13"><span class="cite-bracket">[</span>13<span class="cite-bracket">]</span></a></sup> include <i>view patterns</i>, where a user-defined function transforms the portion of the discriminant corresponding to the position of the view pattern before continuing the match. View patterns generalize predicate patterns, allowing further matching on the result of the function rather than simply expecting a boolean value.</dd></dl>
<dl><dt>Constraint</dt>
<dd>Some pattern languages allow direct comparison of portions of the discriminant with previously-computed (or constant) data structures. For example, the pattern <code class="mw-highlight mw-highlight-lang-rkt mw-content-ltr" style="" dir="ltr"><span class="p">(</span><span class="k">==</span><span class="w"> </span><span class="n">expr</span><span class="p">)</span></code> in Racket compares the value against the result of evaluating <code>expr</code>. In Erlang, mention of any variable already in scope in a pattern causes it to act as a constraint in this way (instead of as a binding).</dd></dl>
<dl><dt>Literal pattern; atomic pattern</dt>
<dd>Patterns that match simple atomic data such as <code>123</code> or <code>"hello"</code> are called <i>literal patterns</i>.</dd></dl>
<dl><dt>Compound pattern</dt>
<dd>Patterns that destructure compound values such as lists, hash tables, tuples, structures or records, with sub-patterns for each of the values making up the compound data structure, are called <i>compound patterns</i>.</dd></dl>
<dl><dt>Alternative (<code>or</code>-pattern)</dt>
<dd>Many languages allow multiple alternatives at the top-level of a pattern matching construct, each associated with a <i>continuation</i>; some languages allow alternatives <i>within</i> a pattern. In most cases, such alternatives have additional constraints placed on them: for example, every alternative may be required to produce the same set of <i>bindings</i> (at the same types).</dd></dl>
<dl><dt>Macros</dt>
<dd>Some languages allow macros in pattern context to allow abstraction over patterns. For example, in Racket, <i>match expanders</i> perform this role.<sup id="cite_ref-14" class="reference"><a href="#cite_note-14"><span class="cite-bracket">[</span>14<span class="cite-bracket">]</span></a></sup></dd></dl>
<div class="mw-heading mw-heading2"><h2 id="Types">Types</h2></div>
<div class="mw-heading mw-heading3"><h3 id="Primitive_patterns">Primitive patterns</h3></div>
<p>The simplest pattern in pattern matching is an explicit value or a variable. For an example, consider a simple function definition in Haskell syntax (function parameters are not in parentheses but are separated by spaces, = is not assignment but definition):
</p>
<div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span class="nf">f</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="mi">1</span>
</pre></div>
<p>Here, 0 is a single value pattern. Now, whenever f is given 0 as argument the pattern matches and the function returns 1. With any other argument, the matching and thus the function fail. As the syntax supports alternative patterns in function definitions, we can continue the definition extending it to take more generic arguments:
</p>
<div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span class="nf">f</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
<p>Here, the first <code>n</code> is a single variable pattern, which will match absolutely any argument and bind it to name n to be used in the rest of the definition. In Haskell (unlike at least <a href="Hope_(programming_language)" title="Hope (programming language)">Hope</a>), patterns are tried in order so the first definition still applies in the very specific case of the input being 0, while for any other argument the function returns <code>n * f (n-1)</code> with n being the argument.
</p><p>The wildcard pattern (often written as <code>_</code>) is also simple: like a variable name, it matches any value, but does not bind the value to any name. Algorithms for <a href="Matching_wildcards" title="Matching wildcards">matching wildcards</a> in simple string-matching situations have been developed in a number of <a href="Recursion" title="Recursion">recursive</a> and non-recursive varieties.<sup id="cite_ref-15" class="reference"><a href="#cite_note-15"><span class="cite-bracket">[</span>15<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="Tree_patterns">Tree patterns</h3></div>
<p>More complex patterns can be built from the primitive ones of the previous section, usually in the same way as values are built by combining other values. The difference then is that with variable and wildcard parts, a pattern does not build into a single value, but matches a group of values that are the combination of the concrete elements and the elements that are allowed to vary within the structure of the pattern.
</p><p>A tree pattern describes a part of a tree by starting with a node and specifying some branches and nodes and leaving some unspecified with a variable or wildcard pattern. It may help to think of the <a href="Abstract_syntax_tree" title="Abstract syntax tree">abstract syntax tree</a> of a programming language and <a href="Algebraic_data_type" title="Algebraic data type">algebraic data types</a>.
</p>
<div class="mw-heading mw-heading4"><h4 id="Haskell">Haskell</h4></div>
<p>In Haskell, the following line defines an algebraic data type <code>Color</code> that has a single data constructor <code>ColorConstructor</code> that wraps an integer and a string.
</p>
<div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span class="kr">data</span><span class="w"> </span><span class="kt">Color</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="kt">ColorConstructor</span><span class="w"> </span><span class="kt">Integer</span><span class="w"> </span><span class="kt">String</span>
</pre></div>
<p>The constructor is a node in a tree and the integer and string are leaves in branches.
</p><p>When we want to write <a href="Function_(programming)" class="mw-redirect" title="Function (programming)">functions</a> to make <code>Color</code> an <a href="Abstract_data_type" title="Abstract data type">abstract data type</a>, we wish to write functions to <a href="Interface_(computer_science)" class="mw-redirect" title="Interface (computer science)">interface</a> with the data type, and thus we want to extract some data from the data type, for example, just the string or just the integer part of <code>Color</code>.
</p><p>If we pass a variable that is of type Color, how can we get the data out of this variable? For example, for a function to get the integer part of <code>Color</code>, we can use a simple tree pattern and write:
</p>
<div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span class="nf">integerPart</span><span class="w"> </span><span class="p">(</span><span class="kt">ColorConstructor</span><span class="w"> </span><span class="n">theInteger</span><span class="w"> </span><span class="kr">_</span><span class="p">)</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="n">theInteger</span>
</pre></div>
<p>As well:
</p>
<div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span class="nf">stringPart</span><span class="w"> </span><span class="p">(</span><span class="kt">ColorConstructor</span><span class="w"> </span><span class="kr">_</span><span class="w"> </span><span class="n">theString</span><span class="p">)</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="n">theString</span>
</pre></div>
<p>The creations of these functions can be automated by Haskell's data <a href="Record_(computer_science)" title="Record (computer science)">record</a> syntax.
</p>
<div class="mw-heading mw-heading4"><h4 id="OCaml">OCaml</h4></div>
<p>This <a href="OCaml" title="OCaml">OCaml</a> example which defines a <a href="Red%E2%80%93black_tree" title="Red–black tree">red–black tree</a> and a function to re-balance it after element insertion shows how to match on a more complex structure generated by a recursive data type. The compiler verifies at compile-time that the list of cases is exhaustive and none are redundant.
</p>
<div class="mw-highlight mw-highlight-lang-ocaml mw-content-ltr" dir="ltr"><pre><span class="k">type</span> <span class="n">color</span> <span class="o">=</span> <span class="nc">Red</span> <span class="o">|</span> <span class="nc">Black</span>
<span class="k">type</span> <span class="k">'</span><span class="n">a</span> <span class="n">tree</span> <span class="o">=</span> <span class="nc">Empty</span> <span class="o">|</span> <span class="nc">Tree</span> <span class="k">of</span> <span class="n">color</span> <span class="o">*</span> <span class="k">'</span><span class="n">a</span> <span class="n">tree</span> <span class="o">*</span> <span class="k">'</span><span class="n">a</span> <span class="o">*</span> <span class="k">'</span><span class="n">a</span> <span class="n">tree</span>
<span class="k">let</span> <span class="n">rebalance</span> <span class="n">t</span> <span class="o">=</span> <span class="k">match</span> <span class="n">t</span> <span class="k">with</span>
<span class="o">|</span> <span class="nc">Tree</span> <span class="o">(</span><span class="nc">Black</span><span class="o">,</span> <span class="nc">Tree</span> <span class="o">(</span><span class="nc">Red</span><span class="o">,</span> <span class="nc">Tree</span> <span class="o">(</span><span class="nc">Red</span><span class="o">,</span> <span class="n">a</span><span class="o">,</span> <span class="n">x</span><span class="o">,</span> <span class="n">b</span><span class="o">),</span> <span class="n">y</span><span class="o">,</span> <span class="n">c</span><span class="o">),</span> <span class="n">z</span><span class="o">,</span> <span class="n">d</span><span class="o">)</span>
<span class="o">|</span> <span class="nc">Tree</span> <span class="o">(</span><span class="nc">Black</span><span class="o">,</span> <span class="nc">Tree</span> <span class="o">(</span><span class="nc">Red</span><span class="o">,</span> <span class="n">a</span><span class="o">,</span> <span class="n">x</span><span class="o">,</span> <span class="nc">Tree</span> <span class="o">(</span><span class="nc">Red</span><span class="o">,</span> <span class="n">b</span><span class="o">,</span> <span class="n">y</span><span class="o">,</span> <span class="n">c</span><span class="o">)),</span> <span class="n">z</span><span class="o">,</span> <span class="n">d</span><span class="o">)</span>
<span class="o">|</span> <span class="nc">Tree</span> <span class="o">(</span><span class="nc">Black</span><span class="o">,</span> <span class="n">a</span><span class="o">,</span> <span class="n">x</span><span class="o">,</span> <span class="nc">Tree</span> <span class="o">(</span><span class="nc">Red</span><span class="o">,</span> <span class="nc">Tree</span> <span class="o">(</span><span class="nc">Red</span><span class="o">,</span> <span class="n">b</span><span class="o">,</span> <span class="n">y</span><span class="o">,</span> <span class="n">c</span><span class="o">),</span> <span class="n">z</span><span class="o">,</span> <span class="n">d</span><span class="o">))</span>
<span class="o">|</span> <span class="nc">Tree</span> <span class="o">(</span><span class="nc">Black</span><span class="o">,</span> <span class="n">a</span><span class="o">,</span> <span class="n">x</span><span class="o">,</span> <span class="nc">Tree</span> <span class="o">(</span><span class="nc">Red</span><span class="o">,</span> <span class="n">b</span><span class="o">,</span> <span class="n">y</span><span class="o">,</span> <span class="nc">Tree</span> <span class="o">(</span><span class="nc">Red</span><span class="o">,</span> <span class="n">c</span><span class="o">,</span> <span class="n">z</span><span class="o">,</span> <span class="n">d</span><span class="o">)))</span>
<span class="o">-></span> <span class="nc">Tree</span> <span class="o">(</span><span class="nc">Red</span><span class="o">,</span> <span class="nc">Tree</span> <span class="o">(</span><span class="nc">Black</span><span class="o">,</span> <span class="n">a</span><span class="o">,</span> <span class="n">x</span><span class="o">,</span> <span class="n">b</span><span class="o">),</span> <span class="n">y</span><span class="o">,</span> <span class="nc">Tree</span> <span class="o">(</span><span class="nc">Black</span><span class="o">,</span> <span class="n">c</span><span class="o">,</span> <span class="n">z</span><span class="o">,</span> <span class="n">d</span><span class="o">))</span>
<span class="o">|</span> <span class="o">_</span> <span class="o">-></span> <span class="n">t</span> <span class="c">(* the 'catch-all' case if no previous pattern matches *)</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="Usage">Usage</h2></div>
<div class="mw-heading mw-heading3"><h3 id="Filtering_data_with_patterns">Filtering data with patterns</h3></div>
<p>Pattern matching can be used to filter data of a certain structure. For instance, in Haskell a <a href="List_comprehension" title="List comprehension">list comprehension</a> could be used for this kind of filtering:
</p>
<div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span class="p">[</span><span class="kt">A</span><span class="w"> </span><span class="n">x</span><span class="o">|</span><span class="kt">A</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="ow"><-</span><span class="w"> </span><span class="p">[</span><span class="kt">A</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="kt">B</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="kt">A</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="kt">B</span><span class="w"> </span><span class="mi">2</span><span class="p">]]</span>
</pre></div>
<p>evaluates to
</p>
<pre>[A 1, A 2]
</pre>
<div class="mw-heading mw-heading3"><h3 id="Pattern_matching_in_Mathematica">Pattern matching in Mathematica</h3></div>
<p>In <a href="Mathematica" class="mw-redirect" title="Mathematica">Mathematica</a>, the only structure that exists is the <a href="Tree_(data_structure)" class="mw-redirect" title="Tree (data structure)">tree</a>, which is populated by symbols. In the <a href="Haskell" title="Haskell">Haskell</a> syntax used thus far, this could be defined as
</p>
<div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span class="kr">data</span><span class="w"> </span><span class="kt">SymbolTree</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="kt">Symbol</span><span class="w"> </span><span class="kt">String</span><span class="w"> </span><span class="p">[</span><span class="kt">SymbolTree</span><span class="p">]</span>
</pre></div>
<p>An example tree could then look like
</p>
<div class="mw-highlight mw-highlight-lang-mathematica mw-content-ltr" dir="ltr"><pre><span class="n">Symbol</span><span class="w"> </span><span class="s">"a"</span><span class="w"> </span><span class="p">[</span><span class="n">Symbol</span><span class="w"> </span><span class="s">"b"</span><span class="w"> </span><span class="p">[],</span><span class="w"> </span><span class="n">Symbol</span><span class="w"> </span><span class="s">"c"</span><span class="w"> </span><span class="p">[]]</span>
</pre></div>
<p>In the traditional, more suitable syntax, the symbols are written as they are and the levels of the tree are represented using <code>[]</code>, so that for instance <code>a[b,c]</code> is a tree with a as the parent, and b and c as the children.
</p><p>A pattern in Mathematica involves putting "_" at positions in that tree. For instance, the pattern
</p>
<pre>A[_]
</pre>
<p>will match elements such as A[1], A[2], or more generally A[<i>x</i>] where <i>x</i> is any entity. In this case, <code>A</code> is the concrete element, while <code>_</code> denotes the piece of tree that can be varied. A symbol prepended to <code>_</code> binds the match to that variable name while a symbol appended to <code>_</code> restricts the matches to nodes of that symbol. Note that even blanks themselves are internally represented as <code>Blank[]</code> for <code>_</code> and <code>Blank[x]</code> for <code>_x</code>.
</p><p>The Mathematica function <code>Cases</code> filters elements of the first argument that match the pattern in the second argument:<sup id="cite_ref-16" class="reference"><a href="#cite_note-16"><span class="cite-bracket">[</span>16<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-highlight mw-highlight-lang-mathematica mw-content-ltr" dir="ltr"><pre><span class="n">Cases</span><span class="p">[{</span><span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span><span class="w"> </span><span class="n">b</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span><span class="w"> </span><span class="n">b</span><span class="p">[</span><span class="mi">2</span><span class="p">]},</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="nv">_</span><span class="p">]</span><span class="w"> </span><span class="p">]</span>
</pre></div>
<p>evaluates to
</p>
<div class="mw-highlight mw-highlight-lang-mathematica mw-content-ltr" dir="ltr"><pre><span class="p">{</span><span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">]}</span>
</pre></div>
<p>Pattern matching applies to the <i>structure</i> of expressions. In the example below,
</p>
<div class="mw-highlight mw-highlight-lang-mathematica mw-content-ltr" dir="ltr"><pre><span class="n">Cases</span><span class="p">[</span><span class="w"> </span><span class="p">{</span><span class="n">a</span><span class="p">[</span><span class="n">b</span><span class="p">],</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">],</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">b</span><span class="p">[</span><span class="n">c</span><span class="p">],</span><span class="w"> </span><span class="n">d</span><span class="p">],</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">b</span><span class="p">[</span><span class="n">c</span><span class="p">],</span><span class="w"> </span><span class="n">d</span><span class="p">[</span><span class="n">e</span><span class="p">]],</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">b</span><span class="p">[</span><span class="n">c</span><span class="p">],</span><span class="w"> </span><span class="n">d</span><span class="p">,</span><span class="w"> </span><span class="n">e</span><span class="p">]},</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">b</span><span class="p">[</span><span class="nv">_</span><span class="p">],</span><span class="w"> </span><span class="nv">_</span><span class="p">]</span><span class="w"> </span><span class="p">]</span>
</pre></div>
<p>returns
</p>
<div class="mw-highlight mw-highlight-lang-mathematica mw-content-ltr" dir="ltr"><pre><span class="p">{</span><span class="n">a</span><span class="p">[</span><span class="n">b</span><span class="p">[</span><span class="n">c</span><span class="p">],</span><span class="n">d</span><span class="p">],</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">b</span><span class="p">[</span><span class="n">c</span><span class="p">],</span><span class="n">d</span><span class="p">[</span><span class="n">e</span><span class="p">]]}</span>
</pre></div>
<p>because only these elements will match the pattern <code>a[b[_],_]</code> above.
</p><p>In Mathematica, it is also possible to extract structures as they are created in the course of computation, regardless of how or where they appear. The function <code>Trace</code> can be used to monitor a computation, and return the elements that arise which match a pattern. For example, we can define the <a href="Fibonacci_number" class="mw-redirect" title="Fibonacci number">Fibonacci sequence</a> as
</p>
<div class="mw-highlight mw-highlight-lang-mathematica mw-content-ltr" dir="ltr"><pre><span class="n">fib</span><span class="p">[</span><span class="mi">0</span><span class="o">|</span><span class="mi">1</span><span class="p">]</span><span class="o">:=</span><span class="mi">1</span>
<span class="n">fib</span><span class="p">[</span><span class="nv">n_</span><span class="p">]</span><span class="o">:=</span><span class="w"> </span><span class="n">fib</span><span class="p">[</span><span class="n">n</span><span class="mi">-1</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">fib</span><span class="p">[</span><span class="n">n</span><span class="mi">-2</span><span class="p">]</span>
</pre></div>
<p>Then, we can ask the question: Given fib[3], what is the sequence of recursive Fibonacci calls?
</p>
<div class="mw-highlight mw-highlight-lang-mathematica mw-content-ltr" dir="ltr"><pre><span class="n">Trace</span><span class="p">[</span><span class="n">fib</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span><span class="w"> </span><span class="n">fib</span><span class="p">[</span><span class="nv">_</span><span class="p">]]</span>
</pre></div>
<p>returns a structure that represents the occurrences of the pattern <code>fib[_]</code> in the computational structure:
</p>
<div class="mw-highlight mw-highlight-lang-mathematica mw-content-ltr" dir="ltr"><pre><span class="p">{</span><span class="n">fib</span><span class="p">[</span><span class="mi">3</span><span class="p">],{</span><span class="n">fib</span><span class="p">[</span><span class="mi">2</span><span class="p">],{</span><span class="n">fib</span><span class="p">[</span><span class="mi">1</span><span class="p">]},{</span><span class="n">fib</span><span class="p">[</span><span class="mi">0</span><span class="p">]}},{</span><span class="n">fib</span><span class="p">[</span><span class="mi">1</span><span class="p">]}}</span>
</pre></div>
<div class="mw-heading mw-heading4"><h4 id="Declarative_programming">Declarative programming</h4></div>
<p>In symbolic programming languages, it is easy to have patterns as arguments to functions or as elements of data structures. A consequence of this is the ability to use patterns to declaratively make statements about pieces of data and to flexibly instruct functions how to operate.
</p><p>For instance, the <a href="Mathematica" class="mw-redirect" title="Mathematica">Mathematica</a> function <code>Compile</code> can be used to make more efficient versions of the code. In the following example the details do not particularly matter; what matters is that the subexpression <code>{{com[_], Integer}}</code> instructs <code>Compile</code> that expressions of the form <code>com[_]</code> can be assumed to be <a href="Integer" title="Integer">integers</a> for the purposes of compilation:
</p>
<div class="mw-highlight mw-highlight-lang-mathematica mw-content-ltr" dir="ltr"><pre><span class="n">com</span><span class="p">[</span><span class="nv">i_</span><span class="p">]</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="n">Binomial</span><span class="p">[</span><span class="mi">2</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">]</span>
<span class="n">Compile</span><span class="p">[{</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="nv">_Integer</span><span class="p">}},</span><span class="w"> </span><span class="n">x</span><span class="o">^</span><span class="n">com</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="w"> </span><span class="p">{{</span><span class="n">com</span><span class="p">[</span><span class="nv">_</span><span class="p">],</span><span class="w"> </span><span class="n">Integer</span><span class="p">}}]</span>
</pre></div>
<p>Mailboxes in <a href="Erlang_(programming_language)" title="Erlang (programming language)">Erlang</a> also work this way.
</p><p>The <a href="Curry%E2%80%93Howard_correspondence" title="Curry–Howard correspondence">Curry–Howard correspondence</a> between proofs and programs relates <a href="ML_(programming_language)" title="ML (programming language)">ML</a>-style pattern matching to <a href="Proof_by_cases" class="mw-redirect" title="Proof by cases">case analysis</a> and <a href="Proof_by_exhaustion" title="Proof by exhaustion">proof by exhaustion</a>.
</p>
<div class="mw-heading mw-heading3"><h3 id="Pattern_matching_and_strings">Pattern matching and strings</h3></div>
<p>By far the most common form of pattern matching involves strings of characters. In many programming languages, a particular syntax of strings is used to represent regular expressions, which are patterns describing string characters.
</p><p>However, it is possible to perform some string pattern matching within the same framework that has been discussed throughout this article.
</p>
<div class="mw-heading mw-heading4"><h4 id="Tree_patterns_for_strings">Tree patterns for strings</h4></div>
<p>In Mathematica, strings are represented as trees of root StringExpression and all the characters in order as children of the root. Thus, to match "any amount of trailing characters", a new wildcard ___ is needed in contrast to _ that would match only a single character.
</p><p>In Haskell and <a href="Functional_programming" title="Functional programming">functional programming</a> languages in general, strings are represented as functional <a href="List_(computing)" class="mw-redirect" title="List (computing)">lists</a> of characters. A functional list is defined as an empty list, or an element constructed on an existing list. In Haskell syntax:
</p>
<div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span class="kt">[]</span><span class="w"> </span><span class="c1">-- an empty list</span>
<span class="nf">x</span><span class="kt">:</span><span class="n">xs</span><span class="w"> </span><span class="c1">-- an element x constructed on a list xs</span>
</pre></div>
<p>The structure for a list with some elements is thus <code>element:list</code>. When pattern matching, we assert that a certain piece of data is equal to a certain pattern. For example, in the function:
</p>
<div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span class="nf">head</span><span class="w"> </span><span class="p">(</span><span class="n">element</span><span class="kt">:</span><span class="n">list</span><span class="p">)</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="n">element</span>
</pre></div>
<p>We assert that the first element of <code>head</code>'s argument is called element, and the function returns this. We know that this is the first element because of the way lists are defined, a single element constructed onto a list. This single element must be the first. The empty list would not match the pattern at all, as an empty list does not have a head (the first element that is constructed).
</p><p>In the example, we have no use for <code>list</code>, so we can disregard it, and thus write the function:
</p>
<div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span class="nf">head</span><span class="w"> </span><span class="p">(</span><span class="n">element</span><span class="kt">:</span><span class="kr">_</span><span class="p">)</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="n">element</span>
</pre></div>
<p>The equivalent Mathematica transformation is expressed as
</p>
<pre>head[element, ]:=element
</pre>
<div class="mw-heading mw-heading4"><h4 id="Example_string_patterns">Example string patterns</h4></div>
<p>In Mathematica, for instance,
</p>
<div class="mw-highlight mw-highlight-lang-mathematica mw-content-ltr" dir="ltr"><pre><span class="n">StringExpression</span><span class="p">[</span><span class="s">"a"</span><span class="p">,</span><span class="nv">_</span><span class="p">]</span>
</pre></div>
<p>will match a string that has two characters and begins with "a".
</p><p>The same pattern in Haskell:
</p>
<div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span class="p">[</span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="kr">_</span><span class="p">]</span>
</pre></div>
<p>Symbolic entities can be introduced to represent many different classes of relevant features of a string. For instance,
</p>
<pre>StringExpression[LetterCharacter, DigitCharacter]
</pre>
<p>will match a string that consists of a letter first, and then a number.
</p><p>In Haskell, <a href="Guard_(computer_science)" title="Guard (computer science)">guards</a> could be used to achieve the same matches:
</p>
<div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span class="p">[</span><span class="n">letter</span><span class="p">,</span><span class="w"> </span><span class="n">digit</span><span class="p">]</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">isAlpha</span><span class="w"> </span><span class="n">letter</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">isDigit</span><span class="w"> </span><span class="n">digit</span>
</pre></div>
<p>The main advantage of symbolic string manipulation is that it can be completely integrated with the rest of the programming language, rather than being a separate, special purpose subunit. The entire power of the language can be leveraged to build up the patterns themselves or analyze and transform the programs that contain them.
</p>
<div class="mw-heading mw-heading4"><h4 id="SNOBOL">SNOBOL</h4></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="SNOBOL" title="SNOBOL">SNOBOL</a></div>
<p>SNOBOL (<i>StriNg Oriented and symBOlic Language</i>) is a computer programming language developed between 1962 and 1967 at <a href="AT%26T_Corporation" title="AT&T Corporation">AT&T</a> <a href="Bell_Laboratories" class="mw-redirect" title="Bell Laboratories">Bell Laboratories</a> by <a href="David_J._Farber" title="David J. Farber">David J. Farber</a>, <a href="Ralph_E._Griswold" class="mw-redirect" title="Ralph E. Griswold">Ralph E. Griswold</a> and Ivan P. Polonsky.
</p><p>SNOBOL4 stands apart from most programming languages by having patterns as a <a href="First-class_object" class="mw-redirect" title="First-class object">first-class data type</a> (<i>i.e.</i> a data type whose values can be manipulated in all ways permitted to any other data type in the programming language) and by providing operators for pattern <a href="Concatenation" title="Concatenation">concatenation</a> and <a href="Alternation_(formal_language_theory)" title="Alternation (formal language theory)">alternation</a>. Strings generated during execution can be treated as programs and executed.
</p><p>SNOBOL was quite widely taught in larger US universities in the late 1960s and early 1970s and was widely used in the 1970s and 1980s as a text manipulation language in the <a href="Humanities" title="Humanities">humanities</a>.
</p><p>Since SNOBOL's creation, newer languages such as <a href="AWK" title="AWK">AWK</a> and <a href="Perl" title="Perl">Perl</a> have made string manipulation by means of <a href="Regular_expression" title="Regular expression">regular expressions</a> fashionable. SNOBOL4 patterns, however, subsume <a href="Backus%E2%80%93Naur_form" title="Backus–Naur form">Backus–Naur form</a> (BNF) grammars, which are equivalent to <a href="Context-free_grammar" title="Context-free grammar">context-free grammars</a> and more powerful than <a href="Regular_expression" title="Regular expression">regular expressions</a>.<sup id="cite_ref-17" class="reference"><a href="#cite_note-17"><span class="cite-bracket">[</span>17<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1266661725">
/* start https://en.wikipedia.org/ */
.mw-parser-output .portalbox{padding:0;margin:0.5em 0;display:table;box-sizing:border-box;max-width:175px;list-style:none}.mw-parser-output .portalborder{border:1px solid var(--border-color-base,#a2a9b1);padding:0.1em;background:var(--background-color-neutral-subtle,#f8f9fa)}.mw-parser-output .portalbox-entry{display:table-row;font-size:85%;line-height:110%;height:1.9em;font-style:italic;font-weight:bold}.mw-parser-output .portalbox-image{display:table-cell;padding:0.2em;vertical-align:middle;text-align:center}.mw-parser-output .portalbox-link{display:table-cell;padding:0.2em 0.2em 0.2em 0.3em;vertical-align:middle}@media(min-width:720px){.mw-parser-output .portalleft{margin:0.5em 1em 0.5em 0}.mw-parser-output .portalright{clear:right;float:right;margin:0.5em 0 0.5em 1em}}
/* end https://en.wikipedia.org/ */
</style>
<ul><li><a href="Artificial_Intelligence_Markup_Language" title="Artificial Intelligence Markup Language">Artificial Intelligence Markup Language</a> (AIML) for an AI language based on matching patterns in speech</li>
<li><a href="AWK" title="AWK">AWK</a> language</li>
<li><a href="Coccinelle_(software)" title="Coccinelle (software)">Coccinelle</a> pattern matches C source code</li>
<li><a href="Matching_wildcards" title="Matching wildcards">Matching wildcards</a></li>
<li><a href="Glob_(programming)" title="Glob (programming)">glob (programming)</a></li>
<li><a href="Pattern_calculus" title="Pattern calculus">Pattern calculus</a></li>
<li><a href="Pattern_recognition" title="Pattern recognition">Pattern recognition</a> for fuzzy patterns</li>
<li><a href="PCRE" class="mw-redirect" title="PCRE">PCRE</a> Perl Compatible Regular Expressions, a common modern implementation of string pattern matching ported to many languages</li>
<li><a href="REBOL" class="mw-redirect" title="REBOL">REBOL parse dialect</a> for pattern matching used to implement language dialects</li>
<li><a href="Symbolic_integration" title="Symbolic integration">Symbolic integration</a></li>
<li><a href="Tagged_union" title="Tagged union">Tagged union</a></li>
<li><a href="Tom_(pattern_matching_language)" class="mw-redirect" title="Tom (pattern matching language)">Tom (pattern matching language)</a></li>
<li><a href="SNOBOL" title="SNOBOL">SNOBOL</a> for a programming language based on one kind of pattern matching</li>
<li><a href="Pattern_language" title="Pattern language">Pattern language</a> — metaphoric, drawn from architecture</li>
<li><a href="Graph_matching" title="Graph matching">Graph matching</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239549316">
/* start https://en.wikipedia.org/ */
.mw-parser-output .refbegin{margin-bottom:0.5em}.mw-parser-output .refbegin-hanging-indents>ul{margin-left:0}.mw-parser-output .refbegin-hanging-indents>ul>li{margin-left:0;padding-left:3.2em;text-indent:-3.2em}.mw-parser-output .refbegin-hanging-indents ul,.mw-parser-output .refbegin-hanging-indents ul li{list-style:none}@media(max-width:720px){.mw-parser-output .refbegin-hanging-indents>ul>li{padding-left:1.6em;text-indent:-1.6em}}.mw-parser-output .refbegin-columns{margin-top:0.3em}.mw-parser-output .refbegin-columns ul{margin-top:0}.mw-parser-output .refbegin-columns li{page-break-inside:avoid;break-inside:avoid-column}@media screen{.mw-parser-output .refbegin{font-size:90%}}
/* end https://en.wikipedia.org/ */
</style><div class="refbegin" style="">
<ul><li>The Mathematica Book, chapter <a rel="nofollow" class="external text" href="https://web.archive.org/web/20050408135452/http://documents.wolfram.com/mathematica/book/section-2.3">Section 2.3: Patterns</a></li>
<li>The Haskell 98 Report, chapter <a rel="nofollow" class="external text" href="http://haskell.org/onlinereport/exps.html#pattern-matching">3.17 Pattern Matching</a>.</li>
<li>Python Reference Manual, chapter <a rel="nofollow" class="external text" href="https://docs.python.org/2/reference/simple_stmts.html#assignment-statements">6.3 Assignment statements</a>.</li>
<li>The <a href="Pure_(programming_language)" title="Pure (programming language)">Pure</a> Programming Language, chapter <a rel="nofollow" class="external text" href="https://web.archive.org/web/20110711112227/http://pure-lang.googlecode.com/svn/docs/pure-intro/pure-intro.pdf">4.3: Patterns</a></li></ul>
</div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */
.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}
/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap mw-references-columns"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */
.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}
/* end https://en.wikipedia.org/ */
</style><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.microsoft.com/en-us/dotnet/csharp/pattern-matching">"Pattern Matching - C# Guide"</a>. 13 March 2024.</cite></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/pattern-matching">"Pattern Matching - F# Guide"</a>. 5 November 2021.</cite></span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://www.haskell.org/tutorial/patterns.html">A Gentle Introduction to Haskell: Patterns</a></span>
</li>
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external free" href="https://docs.oracle.com/en/java/javase/21/language/pattern-matching.html">https://docs.oracle.com/en/java/javase/21/language/pattern-matching.html</a></span>
</li>
<li id="cite_note-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-5">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.python.org/3.10/whatsnew/3.10.html#pep-634-structural-pattern-matching">"What's New In Python 3.10 — Python 3.10.0b3 documentation"</a>. <i>docs.python.org</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2021-07-06</span></span>.</cite></span>
</li>
<li id="cite_note-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-6">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.racket-lang.org/reference/match.html">"Pattern Matching"</a>. <i>docs.racket-lang.org</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2025-06-25</span></span>.</cite></span>
</li>
<li id="cite_note-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-7">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.ruby-lang.org/en/3.0.0/doc/syntax/pattern_matching_rdoc.html">"pattern_matching - Documentation for Ruby 3.0.0"</a>. <i>docs.ruby-lang.org</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2021-07-06</span></span>.</cite></span>
</li>
<li id="cite_note-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-8">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html">"Pattern Syntax - The Rust Programming Language"</a>.</cite></span>
</li>
<li id="cite_note-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-9">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.scala-lang.org/tour/pattern-matching.html">"Pattern Matching"</a>. <i>Scala Documentation</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2021-01-17</span></span>.</cite></span>
</li>
<li id="cite_note-10"><span class="mw-cite-backlink"><b><a href="#cite_ref-10">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.swift.org/swift-book/ReferenceManual/Patterns.html">"Patterns — The Swift Programming Language (Swift 5.1)"</a>.</cite></span>
</li>
<li id="cite_note-11"><span class="mw-cite-backlink"><b><a href="#cite_ref-11">^</a></b></span> <span class="reference-text">Joel Moses, "Symbolic Integration", MIT Project MAC MAC-TR-47, December 1967</span>
</li>
<li id="cite_note-12"><span class="mw-cite-backlink"><b><a href="#cite_ref-12">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/view_patterns.html">"View Patterns - Glasgow Haskell Compiler User's Guide"</a>.</cite></span>
</li>
<li id="cite_note-13"><span class="mw-cite-backlink"><b><a href="#cite_ref-13">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.racket-lang.org/reference/match.html#(idx._(gentag._255._(lib._scribblings/reference/reference..scrbl)))">"Pattern Matching: app"</a>.</cite></span>
</li>
<li id="cite_note-14"><span class="mw-cite-backlink"><b><a href="#cite_ref-14">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.racket-lang.org/reference/match.html#%28form._%28%28lib._racket%2Fmatch..rkt%29._define-match-expander%29%29">"Pattern Matching: define-match-expander"</a>.</cite></span>
</li>
<li id="cite_note-15"><span class="mw-cite-backlink"><b><a href="#cite_ref-15">^</a></b></span> <span class="reference-text"><cite id="CITEREFCantatore2003" class="citation web cs1">Cantatore, Alessandro (2003). <a rel="nofollow" class="external text" href="http://xoomer.virgilio.it/acantato/dev/wildcard/wildmatch.html">"Wildcard matching algorithms"</a>.</cite></span>
</li>
<li id="cite_note-16"><span class="mw-cite-backlink"><b><a href="#cite_ref-16">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://reference.wolfram.com/language/ref/Cases.html.en">"Cases—Wolfram Language Documentation"</a>. <i>reference.wolfram.com</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2020-11-17</span></span>.</cite></span>
</li>
<li id="cite_note-17"><span class="mw-cite-backlink"><b><a href="#cite_ref-17">^</a></b></span> <span class="reference-text">Gimpel, J. F. 1973. A theory of discrete patterns and their implementation in SNOBOL4. Commun. ACM 16, 2 (Feb. 1973), 91–100. DOI=<a rel="nofollow" class="external free" href="http://doi.acm.org/10.1145/361952.361960">http://doi.acm.org/10.1145/361952.361960</a>.</span>
</li>
</ol></div></div>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1290876196">
/* start https://en.wikipedia.org/ */
.mw-parser-output .side-box{margin:4px 0;box-sizing:border-box;border:1px solid #aaa;font-size:88%;line-height:1.25em;background-color:var(--background-color-interactive-subtle,#f8f9fa);display:flow-root}.mw-parser-output .infobox .side-box{font-size:100%}.mw-parser-output .side-box-abovebelow,.mw-parser-output .side-box-text{padding:0.25em 0.9em}.mw-parser-output .side-box-image{padding:2px 0 2px 0.9em;text-align:center}.mw-parser-output .side-box-imageright{padding:2px 0.9em 2px 0;text-align:center}@media(min-width:500px){.mw-parser-output .side-box-flex{display:flex;align-items:center}.mw-parser-output .side-box-text{flex:1;min-width:0}}@media(min-width:720px){.mw-parser-output .side-box{width:238px}.mw-parser-output .side-box-right{clear:right;float:right;margin-left:1em}.mw-parser-output .side-box-left{margin-right:1em}}
/* end https://en.wikipedia.org/ */
</style><style data-mw-deduplicate="TemplateStyles:r1237033735">
/* start https://en.wikipedia.org/ */
@media print{body.ns-0 .mw-parser-output .sistersitebox{display:none!important}}@media screen{html.skin-theme-clientpref-night .mw-parser-output .sistersitebox img[src*="Wiktionary-logo-en-v2.svg"]{background-color:white}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .sistersitebox img[src*="Wiktionary-logo-en-v2.svg"]{background-color:white}}
/* end https://en.wikipedia.org/ */
</style><div class="side-box side-box-right sistersitebox"><style data-mw-deduplicate="TemplateStyles:r1126788409">
/* start https://en.wikipedia.org/ */
.mw-parser-output .plainlist ol,.mw-parser-output .plainlist ul{line-height:inherit;list-style:none;margin:0;padding:0}.mw-parser-output .plainlist ol li,.mw-parser-output .plainlist ul li{margin-bottom:0}
/* end https://en.wikipedia.org/ */
</style>
<div class="side-box-flex">
<div class="side-box-image"><span class="noviewer" typeof="mw:File"></span></div>
<div class="side-box-text plainlist">The Wikibook <i><a href="https://en.wikibooks.org/wiki/Haskell" class="extiw external" title="wikibooks:Haskell">Haskell</a></i> has a page on the topic of: <i><b><a href="https://en.wikibooks.org/wiki/Haskell/Pattern_matching" class="extiw external" title="wikibooks:Haskell/Pattern matching">Pattern matching</a></b></i></div></div>
</div>
<div class="side-box side-box-right sistersitebox">
<div class="side-box-flex">
<div class="side-box-image"><span class="noviewer" typeof="mw:File"></span></div>
<div class="side-box-text plainlist">Wikimedia Commons has media related to <span style="font-weight: bold; font-style: italic;"><a href="https://commons.wikimedia.org/wiki/Category:Pattern_matching" class="extiw external" title="commons:Category:Pattern matching">Pattern matching</a></span>.</div></div>
</div>
<ul><li><a rel="nofollow" class="external text" href="https://archive.today/19990225161739/http://www.haskell.org/development/views.html">Views: An Extension to Haskell Pattern Matching</a></li>
<li>Nikolaas N. Oosterhof, Philip K. F. Hölzenspies, and Jan Kuper. <a rel="nofollow" class="external text" href="https://web.archive.org/web/20060304053330/http://wwwhome.cs.utwente.nl/~tina/apm/applPatts.pdf">Application patterns</a>. A presentation at Trends in Functional Programming, 2005</li>
<li><a rel="nofollow" class="external text" href="http://www.cs.cornell.edu/Projects/jmatch">JMatch</a>: the <a href="Java_(programming_language)" title="Java (programming language)">Java</a> language extended with pattern matching</li>
<li><a rel="nofollow" class="external text" href="https://archive.today/20130630081135/http://www.showtrend.com/">ShowTrend</a>: Online pattern matching for stock prices</li>
<li><a rel="nofollow" class="external text" href="https://web.archive.org/web/20060211020429/http://cm.bell-labs.com/cm/cs/who/dmr/qed.html">An incomplete history of the QED Text Editor</a> by <a href="Dennis_Ritchie" title="Dennis Ritchie">Dennis Ritchie</a> - provides the history of regular expressions in computer programs</li>
<li><a rel="nofollow" class="external text" href="http://research.microsoft.com/~simonpj/papers/slpj-book-1987/index.htm">The Implementation of Functional Programming Languages, pages 53–103</a> Simon Peyton Jones, published by Prentice Hall, 1987.</li>
<li><a rel="nofollow" class="external text" href="https://github.com/rsdn/nemerle/wiki/Grok-Variants-and-matching#matching">Nemerle, pattern matching</a>.</li>
<li><a rel="nofollow" class="external text" href="http://erlang.org/doc/reference_manual/expressions.html#pattern">Erlang, pattern matching</a>.</li>
<li><a rel="nofollow" class="external text" href="https://web.archive.org/web/20090822225301/http://www.cs.nyu.edu/leunga/prop.html">Prop: a C++ based pattern matching language, 1999</a></li>
<li><a rel="nofollow" class="external text" href="https://github.com/Henry/PatMat">PatMat: a C++ pattern matching library based on</a> <a href="SNOBOL" title="SNOBOL">SNOBOL</a>/<a href="SPITBOL" title="SPITBOL">SPITBOL</a></li>
<li>Temur Kutsia. <a rel="nofollow" class="external text" href="https://dx.doi.org/10.1016/j.jsc.2008.05.001">Flat Matching</a>. Journal of Symbolic Computation 43(12): 858–873. Describes in details flat matching in Mathematica.</li>
<li><a rel="nofollow" class="external text" href="http://www.datamystic.com/easypatterns_reference.html">EasyPattern language</a> pattern matching language for non-programmers</li></ul>
<div class="navbox-styles"><style data-mw-deduplicate="TemplateStyles:r1129693374">
/* start https://en.wikipedia.org/ */
.mw-parser-output .hlist dl,.mw-parser-output .hlist ol,.mw-parser-output .hlist ul{margin:0;padding:0}.mw-parser-output .hlist dd,.mw-parser-output .hlist dt,.mw-parser-output .hlist li{margin:0;display:inline}.mw-parser-output .hlist.inline,.mw-parser-output .hlist.inline dl,.mw-parser-output .hlist.inline ol,.mw-parser-output .hlist.inline ul,.mw-parser-output .hlist dl dl,.mw-parser-output .hlist dl ol,.mw-parser-output .hlist dl ul,.mw-parser-output .hlist ol dl,.mw-parser-output .hlist ol ol,.mw-parser-output .hlist ol ul,.mw-parser-output .hlist ul dl,.mw-parser-output .hlist ul ol,.mw-parser-output .hlist ul ul{display:inline}.mw-parser-output .hlist .mw-empty-li{display:none}.mw-parser-output .hlist dt::after{content:": "}.mw-parser-output .hlist dd::after,.mw-parser-output .hlist li::after{content:" · ";font-weight:bold}.mw-parser-output .hlist dd:last-child::after,.mw-parser-output .hlist dt:last-child::after,.mw-parser-output .hlist li:last-child::after{content:none}.mw-parser-output .hlist dd dd:first-child::before,.mw-parser-output .hlist dd dt:first-child::before,.mw-parser-output .hlist dd li:first-child::before,.mw-parser-output .hlist dt dd:first-child::before,.mw-parser-output .hlist dt dt:first-child::before,.mw-parser-output .hlist dt li:first-child::before,.mw-parser-output .hlist li dd:first-child::before,.mw-parser-output .hlist li dt:first-child::before,.mw-parser-output .hlist li li:first-child::before{content:" (";font-weight:normal}.mw-parser-output .hlist dd dd:last-child::after,.mw-parser-output .hlist dd dt:last-child::after,.mw-parser-output .hlist dd li:last-child::after,.mw-parser-output .hlist dt dd:last-child::after,.mw-parser-output .hlist dt dt:last-child::after,.mw-parser-output .hlist dt li:last-child::after,.mw-parser-output .hlist li dd:last-child::after,.mw-parser-output .hlist li dt:last-child::after,.mw-parser-output .hlist li li:last-child::after{content:")";font-weight:normal}.mw-parser-output .hlist ol{counter-reset:listitem}.mw-parser-output .hlist ol>li{counter-increment:listitem}.mw-parser-output .hlist ol>li::before{content:" "counter(listitem)"\a0 "}.mw-parser-output .hlist dd ol>li:first-child::before,.mw-parser-output .hlist dt ol>li:first-child::before,.mw-parser-output .hlist li ol>li:first-child::before{content:" ("counter(listitem)"\a0 "}
/* end https://en.wikipedia.org/ */
</style><style data-mw-deduplicate="TemplateStyles:r1236075235">
/* start https://en.wikipedia.org/ */
.mw-parser-output .navbox{box-sizing:border-box;border:1px solid #a2a9b1;width:100%;clear:both;font-size:88%;text-align:center;padding:1px;margin:1em auto 0}.mw-parser-output .navbox .navbox{margin-top:0}.mw-parser-output .navbox+.navbox,.mw-parser-output .navbox+.navbox-styles+.navbox{margin-top:-1px}.mw-parser-output .navbox-inner,.mw-parser-output .navbox-subgroup{width:100%}.mw-parser-output .navbox-group,.mw-parser-output .navbox-title,.mw-parser-output .navbox-abovebelow{padding:0.25em 1em;line-height:1.5em;text-align:center}.mw-parser-output .navbox-group{white-space:nowrap;text-align:right}.mw-parser-output .navbox,.mw-parser-output .navbox-subgroup{background-color:#fdfdfd}.mw-parser-output .navbox-list{line-height:1.5em;border-color:#fdfdfd}.mw-parser-output .navbox-list-with-group{text-align:left;border-left-width:2px;border-left-style:solid}.mw-parser-output tr+tr>.navbox-abovebelow,.mw-parser-output tr+tr>.navbox-group,.mw-parser-output tr+tr>.navbox-image,.mw-parser-output tr+tr>.navbox-list{border-top:2px solid #fdfdfd}.mw-parser-output .navbox-title{background-color:#ccf}.mw-parser-output .navbox-abovebelow,.mw-parser-output .navbox-group,.mw-parser-output .navbox-subgroup .navbox-title{background-color:#ddf}.mw-parser-output .navbox-subgroup .navbox-group,.mw-parser-output .navbox-subgroup .navbox-abovebelow{background-color:#e6e6ff}.mw-parser-output .navbox-even{background-color:#f7f7f7}.mw-parser-output .navbox-odd{background-color:transparent}.mw-parser-output .navbox .hlist td dl,.mw-parser-output .navbox .hlist td ol,.mw-parser-output .navbox .hlist td ul,.mw-parser-output .navbox td.hlist dl,.mw-parser-output .navbox td.hlist ol,.mw-parser-output .navbox td.hlist ul{padding:0.125em 0}.mw-parser-output .navbox .navbar{display:block;font-size:100%}.mw-parser-output .navbox-title .navbar{float:left;text-align:left;margin-right:0.5em}body.skin--responsive .mw-parser-output .navbox-image img{max-width:none!important}@media print{body.ns-0 .mw-parser-output .navbox{display:none!important}}
/* end https://en.wikipedia.org/ */
</style></div><div role="navigation" class="navbox" aria-labelledby="Strings176" style="padding:3px"><table class="nowraplinks mw-collapsible mw-collapsed navbox-inner" style="border-spacing:0;background:transparent;color:inherit"><tbody><tr><th scope="col" class="navbox-title" colspan="2"><style data-mw-deduplicate="TemplateStyles:r1239400231">
/* start https://en.wikipedia.org/ */
.mw-parser-output .navbar{display:inline;font-size:88%;font-weight:normal}.mw-parser-output .navbar-collapse{float:left;text-align:left}.mw-parser-output .navbar-boxtext{word-spacing:0}.mw-parser-output .navbar ul{display:inline-block;white-space:nowrap;line-height:inherit}.mw-parser-output .navbar-brackets::before{margin-right:-0.125em;content:"[ "}.mw-parser-output .navbar-brackets::after{margin-left:-0.125em;content:" ]"}.mw-parser-output .navbar li{word-spacing:-0.125em}.mw-parser-output .navbar a>span,.mw-parser-output .navbar a>abbr{text-decoration:inherit}.mw-parser-output .navbar-mini abbr{font-variant:small-caps;border-bottom:none;text-decoration:none;cursor:inherit}.mw-parser-output .navbar-ct-full{font-size:114%;margin:0 7em}.mw-parser-output .navbar-ct-mini{font-size:114%;margin:0 4em}html.skin-theme-clientpref-night .mw-parser-output .navbar li a abbr{color:var(--color-base)!important}@media(prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .navbar li a abbr{color:var(--color-base)!important}}@media print{.mw-parser-output .navbar{display:none!important}}
/* end https://en.wikipedia.org/ */
</style><div id="Strings176" style="font-size:114%;margin:0 4em"><a href="String_(computer_science)" title="String (computer science)">Strings</a></div></th></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="String_metric" title="String metric">String metric</a></th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Approximate_string_matching" title="Approximate string matching">Approximate string matching</a></li>
<li><a href="Bitap_algorithm" title="Bitap algorithm">Bitap algorithm</a></li>
<li><a href="Damerau%E2%80%93Levenshtein_distance" title="Damerau–Levenshtein distance">Damerau–Levenshtein distance</a></li>
<li><a href="Edit_distance" title="Edit distance">Edit distance</a></li>
<li><a href="Gestalt_pattern_matching" title="Gestalt pattern matching">Gestalt pattern matching</a></li>
<li><a href="Hamming_distance" title="Hamming distance">Hamming distance</a></li>
<li><a href="Jaro%E2%80%93Winkler_distance" title="Jaro–Winkler distance">Jaro–Winkler distance</a></li>
<li><a href="Lee_distance" title="Lee distance">Lee distance</a></li>
<li><a href="Levenshtein_automaton" title="Levenshtein automaton">Levenshtein automaton</a></li>
<li><a href="Levenshtein_distance" title="Levenshtein distance">Levenshtein distance</a></li>
<li><a href="Wagner%E2%80%93Fischer_algorithm" title="Wagner–Fischer algorithm">Wagner–Fischer algorithm </a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="String-searching_algorithm" title="String-searching algorithm">String-searching algorithm</a></th><td class="navbox-list-with-group navbox-list navbox-even hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Apostolico%E2%80%93Giancarlo_algorithm" title="Apostolico–Giancarlo algorithm">Apostolico–Giancarlo algorithm</a></li>
<li><a href="Boyer%E2%80%93Moore_string-search_algorithm" title="Boyer–Moore string-search algorithm">Boyer–Moore string-search algorithm</a></li>
<li><a href="Boyer%E2%80%93Moore%E2%80%93Horspool_algorithm" title="Boyer–Moore–Horspool algorithm">Boyer–Moore–Horspool algorithm</a></li>
<li><a href="Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm" title="Knuth–Morris–Pratt algorithm">Knuth–Morris–Pratt algorithm</a></li>
<li><a href="Rabin%E2%80%93Karp_algorithm" title="Rabin–Karp algorithm">Rabin–Karp algorithm</a></li>
<li><a href="Raita_algorithm" title="Raita algorithm">Raita algorithm</a></li>
<li><a href="Trigram_search" title="Trigram search">Trigram search</a></li>
<li><a href="Two-way_string-matching_algorithm" title="Two-way string-matching algorithm">Two-way string-matching algorithm</a></li>
<li><a href="Zhu%E2%80%93Takaoka_string_matching_algorithm" title="Zhu–Takaoka string matching algorithm">Zhu–Takaoka string matching algorithm</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Multiple string searching</th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Aho%E2%80%93Corasick_algorithm" title="Aho–Corasick algorithm">Aho–Corasick</a></li>
<li><a href="Commentz-Walter_algorithm" title="Commentz-Walter algorithm">Commentz-Walter algorithm</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="Regular_expression" title="Regular expression">Regular expression</a></th><td class="navbox-list-with-group navbox-list navbox-even hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Comparison_of_regular-expression_engines" class="mw-redirect" title="Comparison of regular-expression engines">Comparison of regular-expression engines</a></li>
<li><a href="Regular_grammar" title="Regular grammar">Regular grammar</a></li>
<li><a href="Thompson's_construction" title="Thompson's construction">Thompson's construction</a></li>
<li><a href="Nondeterministic_finite_automaton" title="Nondeterministic finite automaton">Nondeterministic finite automaton</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="Sequence_alignment" title="Sequence alignment">Sequence alignment</a></th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="BLAST_(biotechnology)" title="BLAST (biotechnology)">BLAST</a></li>
<li><a href="Hirschberg's_algorithm" title="Hirschberg's algorithm">Hirschberg's algorithm</a></li>
<li><a href="Needleman%E2%80%93Wunsch_algorithm" title="Needleman–Wunsch algorithm">Needleman–Wunsch algorithm</a></li>
<li><a href="Smith%E2%80%93Waterman_algorithm" title="Smith–Waterman algorithm">Smith–Waterman algorithm</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="Data_structure" title="Data structure">Data structure</a></th><td class="navbox-list-with-group navbox-list navbox-even hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Deterministic_acyclic_finite_state_automaton" title="Deterministic acyclic finite state automaton">DAFSA</a></li>
<li><a href="Substring_index" title="Substring index">Substring index</a>
<ul><li><a href="Suffix_array" title="Suffix array">Suffix array</a></li>
<li><a href="Suffix_automaton" title="Suffix automaton">Suffix automaton</a></li>
<li><a href="Suffix_tree" title="Suffix tree">Suffix tree</a></li>
<li><a href="Compressed_suffix_array" title="Compressed suffix array">Compressed suffix array</a></li>
<li><a href="LCP_array" title="LCP array">LCP array</a></li>
<li><a href="FM-index" title="FM-index">FM-index</a></li></ul></li>
<li><a href="Generalized_suffix_tree" title="Generalized suffix tree">Generalized suffix tree</a></li>
<li><a href="Rope_(data_structure)" title="Rope (data structure)">Rope</a></li>
<li><a href="Ternary_search_tree" title="Ternary search tree">Ternary search tree</a></li>
<li><a href="Trie" title="Trie">Trie</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Other</th><td class="navbox-list-with-group navbox-list navbox-odd hlist" style="width:100%;padding:0"><div style="padding:0 0.25em">
<ul><li><a href="Parsing" title="Parsing">Parsing</a></li>
<li><a href="Compressed_pattern_matching" title="Compressed pattern matching">Compressed pattern matching</a></li>
<li><a href="Longest_common_subsequence" title="Longest common subsequence">Longest common subsequence</a></li>
<li><a href="Longest_common_substring" title="Longest common substring">Longest common substring</a></li>
<li><a href="Sequential_pattern_mining" title="Sequential pattern mining">Sequential pattern mining</a></li>
<li>Sorting</li>
<li><a href="Semi-Thue_system" title="Semi-Thue system">String rewriting systems</a></li>
<li><a href="String_operations" title="String operations">String operations</a></li></ul>
</div></td></tr></tbody></table></div>
<div class="navbox-styles"></div><div role="navigation" class="navbox authority-control" aria-label="Navbox391" style="padding:3px"><table class="nowraplinks hlist navbox-inner" style="border-spacing:0;background:transparent;color:inherit"><tbody><tr><th scope="row" class="navbox-group" style="width:1%">Authority control databases: National </th><td class="navbox-list-with-group navbox-list navbox-odd" style="width:100%;padding:0"><div style="padding:0 0.25em"><ul><li><span class="uid"><a rel="nofollow" class="external text" href="https://d-nb.info/gnd/4307192-2">Germany</a></span></li></ul></div></td></tr></tbody></table></div></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-06-25" href="https://en.wikipedia.org/wiki/?title=Pattern_matching&oldid=1297295456">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>